package com.atguigu.gulimall.controller;

import com.atguigu.gulimall.inteceptor.CartInteceptor;
import com.atguigu.gulimall.service.CartService;
import com.atguigu.gulimall.to.UserInfoTo;
import com.atguigu.gulimall.vo.Cart;
import com.atguigu.gulimall.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CartController {


    @Autowired
    private CartService cartService;

    @GetMapping("/getCurrentUserCartItem")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItem(){
        List<CartItem> list = cartService.getCurrentUserCartItem();
        return list;
    }

    // ThreadLocal 同一个线程共享数据
    @GetMapping("/cart.html")
    public String cartListPage(Model model) {
        Cart cart = cartService.getCart();
        System.out.println("总价："+cart.getTotalAmount());
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /** RedirectAttributes
     *   addAttributes() 表示将数据放在url路径后
     *   addFlushAttributes() 表示将数据放在session里面，可以在里面取出，但是只可以取出一次
     *
     * @param skuId
     * @param num
     * @param redirectAttributes
     * @return
     */
    @GetMapping("/addToCart")
    public String index(@RequestParam("skuId")Long skuId,
                        @RequestParam("num")Integer num,
                        RedirectAttributes redirectAttributes) {
        CartItem cartItem = cartService.addToCart(skuId,num);
        redirectAttributes.addAttribute("skuId",skuId);
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId")Long skuId,Model model){
        System.out.println("skuId -> "+skuId);
        CartItem cartItem = cartService.getModelForSkuId(skuId);
        model.addAttribute("item",cartItem);
        return "success";
    }

    @ResponseBody
    @GetMapping("test")
    public String test(HttpSession session){
        String username = (String)session.getAttribute("username");
        System.out.println("username -> "+username);  // yangpeng
        String username2 = (String) session.getAttribute("sessionAttr:username"); // null
        System.out.println("sessionAttr:username -> "+username2);
        return "ok";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId")Long skuId,
                            @RequestParam("check")Integer check){
        cartService.cartItem(skuId,check);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId")Long skuId,
                            @RequestParam("num")Integer num){
        cartService.changeSkuNum(skuId,num);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId")Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
}
